package com.home.base.util;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.home.biz.JobLogBiz;
import com.home.biz.JobUserBiz;
import com.home.config.InitDataConfig;
import com.home.entity.JobLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author ：chenxf
 * @date ：Created in 2020/3/18 10:45
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
public class PhoneFetchUtil {
    private WebClient webClient = new WebClient(BrowserVersion.CHROME);

    public  PhoneFetchUtil(){
        WebClientOptions clientOptions = webClient.getOptions();
        // 设置webClient的相关参数
        clientOptions.setJavaScriptEnabled(false);
        clientOptions.setCssEnabled(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        clientOptions.setTimeout(35000);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        clientOptions.setThrowExceptionOnScriptError(false);
    }

    public void close() {
        webClient.close();
    }

    /**
     * 宝藏卡抓取
     * @return
     * @throws Exception
     */
    public void bckList() throws Exception {
        webClient.getCache().clear();
        String fetchUrl = "https://rwk.cmicrwx.cn/rwx/rwkvue/young/#/infomation?channelId=C10000014363&sellerId=&sellerMobile=&ex_field=&kfcOrderId=&productId=";

        log.info(" 宝藏卡开始");
        HtmlPage htmlPage = webClient.getPage(fetchUrl);
        System.out.println(htmlPage.asXml());
        HtmlElement ulHtml = htmlPage.querySelector(".papart3");
        System.out.println(ulHtml.asXml());
        //ulHtml.querySelector("li")
        List<HtmlButton> buttons = htmlPage.getByXPath("//button");

        List<HtmlElement> titleElements = htmlPage.getByXPath("//a/dl/dt");

    }

    public static void main(String[] args){
        PhoneFetchUtil util=new PhoneFetchUtil();
        try {
            util.bckList();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
